package com.wangijun.yuan.admin.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.wangijun.yuan.admin.base.CommonPage;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.admin.system.domain.SysMenu;
import com.wangijun.yuan.admin.system.model.dto.MenuSaveParam;
import com.wangijun.yuan.admin.system.model.dto.MenuUpdateParam;
import com.wangijun.yuan.admin.system.model.vo.MenuVO;
import com.wangijun.yuan.admin.system.service.SysMenuService;
import com.wangijun.yuan.core.model.IdParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

@Tag(name = "菜单相关接口")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    SysMenuService menuService;


    /**
     * 功能描述: 列表数据
     *
     * @author wlf 2023/9/15 23:05
     */
    @Operation(summary = "查", description = "查")
    @GetMapping("/list")
    CommonResult<CommonPage<Tree<Long>>> list() {
        return CommonResult.data(menuService.getList());
    }

    /**
     * 功能描述: 添加操作
     *
     * @author wlf 2023/9/15 23:05
     */
    @Operation(summary = "增", description = "增")
    @PostMapping("/add")
    CommonResult<Boolean> add(@RequestBody MenuSaveParam param) {
        menuService.add(param);
        return CommonResult.data(true);
    }

    /**
     * 功能描述: 编辑具体操作
     *
     * @author wlf 2023/9/15 23:05
     */
    @Operation(summary = "改", description = "改")
    @PostMapping("/update")
    CommonResult<Boolean> update(@RequestBody MenuUpdateParam param) {
        menuService.update(param);
        return CommonResult.data(true);
    }

    /**
     * 功能描述: 删除表单
     *
     * @author wlf 2023/9/15 23:06
     */
    @Operation(summary = "删", description = "删")
    @PostMapping("/del")
    CommonResult<Boolean> del(@RequestBody IdParam param) {
        menuService.del(param.getId());
        return CommonResult.data(true);
    }

    @Operation(summary = "ByID查询", description = "ByID查询")
    @PostMapping("/get")
    CommonResult<MenuVO> get(@RequestBody IdParam param) {
        SysMenu sysMenu = menuService.get(param.getId());
        MenuVO menuVO = BeanUtil.copyProperties(sysMenu, MenuVO.class);
        menuVO.setParent(BeanUtil.copyProperties(menuService.get(menuVO.getPid()), MenuVO.class));
        return CommonResult.data(menuVO);
    }
}
